﻿using System;

namespace Microsoft.Xna.Framework.Graphics
{
	/// <summary>
	/// Represents a cubic texture resource that will be written to at the end of a render pass.
	/// </summary>
	public class RenderTargetCube : TextureCube
	{
		#region Fields
		private DepthFormat depthStencilFormat;
		private bool isContentLost;
		private int multiSampleCount;
		private RenderTargetUsage renderTargetUsage;
		#endregion

		#region Properties
		/// <summary>
		/// Gets the depth format of this rendertarget.
		/// </summary>
		public DepthFormat DepthStencilFormat
		{
			get { return depthStencilFormat; }
		}

		/// <summary>
		/// Determines if the data has been lost due to a lost device event.
		/// </summary>
		public bool IsContentLost
		{
			get
			{
				if (!this.isContentLost)
				{
					this.isContentLost = base.GraphicsDevice.IsDeviceLost;
				}
				return this.isContentLost;
			}
		}

		/// <summary>
		/// Gets the number of multisample locations.
		/// </summary>
		public int MultiSampleCount
		{
			get { return multiSampleCount; }
		}

		/// <summary>
		/// Gets the usage mode of this rendertarget.
		/// </summary>
		public RenderTargetUsage RenderTargetUsage
		{
			get { return renderTargetUsage; }
		}
		#endregion

		#region Events
		/// <summary>
		/// Occurs when a resource is lost due to a device being lost.
		/// </summary>
		public event EventHandler<EventArgs> ContentLost;
		#endregion

		#region Constructor
		/// <summary>
		/// 
		/// </summary>
		/// <param name="graphicsDevice"></param>
		/// <param name="size"></param>
		/// <param name="mipMap"></param>
		/// <param name="preferredFormat"></param>
		/// <param name="preferredDepthFormat"></param>
		public RenderTargetCube(GraphicsDevice graphicsDevice, int size,  bool mipMap, SurfaceFormat preferredFormat, DepthFormat preferredDepthFormat)
			: this(graphicsDevice, size, mipMap, preferredFormat, preferredDepthFormat, 0, RenderTargetUsage.DiscardContents)
		{
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="graphicsDevice"></param>
		/// <param name="size"></param>
		/// <param name="mipMap"></param>
		/// <param name="preferredFormat"></param>
		/// <param name="preferredDepthFormat"></param>
		/// <param name="preferredMultiSampleCount"></param>
		/// <param name="usage"></param>
		public RenderTargetCube(GraphicsDevice graphicsDevice, int size,  bool mipMap, SurfaceFormat preferredFormat, DepthFormat preferredDepthFormat, int preferredMultiSampleCount, RenderTargetUsage usage)
		{
			if (graphicsDevice == null)
			{
				throw new ArgumentNullException("graphicsDevice", FrameworkResources.DeviceCannotBeNullOnResourceCreate);
			}

			SurfaceFormat surfaceFormat;
			int num;
			DepthFormat depthFormat;

			graphicsDevice.Adapter.QueryBackBufferFormat(graphicsDevice.GraphicsProfile, preferredFormat, preferredDepthFormat, preferredMultiSampleCount, out surfaceFormat, out depthFormat, out num);
			TextureCube.ValidateCreationParameters(size, surfaceFormat);

			this.depthStencilFormat = depthFormat;
			this.multiSampleCount = num;
			this.renderTargetUsage = usage;
		}
		#endregion
	}
}
